﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.JavaScript.JQuery;
using blackhouse.Web.FasterFiles;

namespace blackhouse.Web.Controls {
    public class Notification : Label {

        #region Properties

        [Category("Notification")]
        [Browsable(true)]
        [DefaultValue("")]
        public string Title { get; set; }

        [Category("Notification")]
        [Browsable(true)]
        [DefaultValue(true)]
        public bool UseInternalStyle { get; set; }

        [Category("Notification")]
        [Browsable(true)]
        [DefaultValue(NotificationType.Info)]
        public NotificationType NotificationType { get; set; }

        [Category("Notification")]
        [Browsable(true)]
        [DefaultValue(500)]
        public int AnimateSpeed { get; set; }

        [Category("Notification")]
        [Browsable(true)]
        [DefaultValue(0)]
        public int TimeoutShow { get; set; }

        #endregion

        #region Methods

        public Notification() {
            this.UseInternalStyle = true;
            this.NotificationType = NotificationType.Info;
            this.AnimateSpeed = 500;
        }

        
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.Page.LoadComplete += Page_LoadComplete;
        }

        void Page_LoadComplete(object sender, EventArgs e) {

            if (this.Visible) {
                JQueryLibraries jql = new JQueryLibraries();
                jql.IncludeJQueryMessages();

                FasterFilesProvider files = new FasterFilesProvider();
                files.AddAfterScript(string.Format("$(function()[ $('#{0}').message([messageType:'{1}', title: '{2}', timeoutShow: {3}, animateSpeed: {4} ]); ]);", this.ClientID, this.NotificationType.ToString().ToLower(), this.Title, this.TimeoutShow, this.AnimateSpeed).ReplaceEx("\r\n", "").ReplaceEx("[", "{").ReplaceEx("]", "}"));

                this.Style.Add(HtmlTextWriterStyle.Display, "none");
            }

        }

        #endregion

    }

    public enum NotificationType {
        Info,
        Warning,
        Error,
        Success
    }

}